package cn.concurrence.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author summit
 * @since 2022/4/21 10:55
 */
public class AtomicStampedReferenceTest {

	public static void main(String[] args) {
		AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100, 1);

		new Thread(() -> {
			int stamp = atomicStampedReference.getStamp();
			System.out.printf("11stamp=>%s,value=>%s%n", stamp, atomicStampedReference.getReference());
			try {
				TimeUnit.SECONDS.sleep(4);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			boolean b = atomicStampedReference.compareAndSet(100, 100, stamp, stamp + 1);
			System.out.println("设值结果" + b);

			System.out.println(String.format("22stamp=>%s,value=>%s", atomicStampedReference.getStamp(), atomicStampedReference.getReference()));

			boolean c = atomicStampedReference.compareAndSet(100, 200, stamp, stamp + 1);
			System.out.println("设值结果" + c);

			System.out.println(String.format("33stamp=>%s,value=>%s", atomicStampedReference.getStamp(), atomicStampedReference.getReference()));

		}, "thread1").start();

		/**
		 * 11stamp=>1,value=>100
		 * 设值结果true
		 * 22stamp=>2,value=>100
		 * 设值结果false
		 * 33stamp=>2,value=>100
		 */
	}
}
